package com.gljx.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gljx.constant.Constant;
import com.gljx.util.CommonUtil;
import com.gljx.web.dao.HandleMapper;
import com.gljx.web.Entity.Handle;
import com.gljx.web.Entity.Menu;
import com.gljx.web.service.HandelServcie;

@Service
public class HandelServcieImpl implements HandelServcie{

	@Autowired
	private HandleMapper handleDao;
	
	@Override
	public List<Handle> selectHandleByUserName(String userName) {
		return handleDao.selectHandleByUserName(userName);
	}

	@Override
	public Map<String, Object> selectBIMenu(String rows, String page, String handelName, String pmenuName,
			String fisDelete) {
		Integer pageNum = Integer.valueOf(page);
		Integer pageSize = Integer.valueOf(rows);
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("Pindex", pageNum);
		String sql=  "select h.ID, h.HANDLE_NAME, h.MENU_ID, "
						+ "h.HANDLE_URL, h.CREAT_TIME, h.UPDATE_TIME, "
						+ "h.CREAT_ID, h.UPDATE_ID, h.REMARK, h.FIS_DELETE,"
						+ "m.menu_name menuName "
						+ "from t_e_handle h "
						+ "left join t_e_menu m on m.id=h.menu_id  where m.projectnumber='"+Constant.Project_ID+"' ";
		if(handelName != null && !"".equals(handelName)){
			sql+= " and h.HANDLE_NAME like '%"+handelName+"%'" ;//操作名
		}
		if(pmenuName != null && !"".equals(pmenuName)){
			sql+= " and m.MENU_NAME like '%"+pmenuName+"%'" ;//所属菜单
		}
		//是否可用
		if(!"no".equals(fisDelete)){
			sql+= " and h.FIS_DELETE ="+fisDelete ;
		}
		
		sql+= " order by h.HANDLE_URL ";
		data.put("Psql",sql);
		data.put("Psize", pageSize);
		
		handleDao.selectBIMenu(data);
		return data;
	}

	@Override
	public Integer insertSelective(Handle record) {
		record.setCreatId((String)SecurityUtils.getSubject().getPrincipal());
		record.setCreatTime(CommonUtil.nowTime());
		record.setProjectnumber(Constant.Project_ID);
		return handleDao.insertSelective(record);
	}

	@Override
	public Handle selectByPrimaryKey(Integer id) {
		return handleDao.selectByPrimaryKey(id,Constant.Project_ID);
	}

	@Override
	public Integer updateByPrimaryKeySelective(Handle record) {
		record.setUpdateId((String)SecurityUtils.getSubject().getPrincipal());
		record.setUpdateTime(CommonUtil.nowTime());
		return handleDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public Integer updateHandelDelete(String ids, String delete) {
		String[] idStrings = ids.split(",");
		List<Handle> handles = new ArrayList<Handle>();
		String updateId = (String)SecurityUtils.getSubject().getPrincipal();
		//处理传入参数
		for(String string : idStrings){
			Handle handle = new Handle();
			//启用停用标记
			handle.setFisDelete(Integer.valueOf(delete));
			//修改人修改时间
			handle.setUpdateId(updateId);
			handle.setUpdateTime(CommonUtil.nowTime());
			//id
			handle.setId(Integer.valueOf(string));
			handles.add(handle);
		}
		return handleDao.updateHandelDelete(handles);
	}

	@Override
	public List<Map<String, Object>> findHandle(String menuName, String PID, String name,String isDelete) {
		List<Map<String, Object>> aList =handleDao.findHandle(menuName, PID, name,isDelete);
		return aList;
	}

}
